package com.team10.hotel.service.impl;

import com.team10.hotel.domain.*;
import com.team10.hotel.mapper.GoodMapper;
import com.team10.hotel.mapper.GoodOrderMapper;
import com.team10.hotel.mapper.SupplierMapper;
import com.team10.hotel.service.SupplierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private GoodOrderMapper goodOrderMapper;
    public List<Supplier> listSupplier(){ return supplierMapper.selectByExample(new SupplierExample());}

    @Override
    public int saveOrUpdate(Supplier supplier) {
        if (supplier.getSupplierId()!=null){
            return supplierMapper.updateByPrimaryKey(supplier);
        }else {
            return supplierMapper.insert(supplier);
        }
    }

    @Override
    public void deleteByPrimaryKey(Integer id) {
        List<Good> goods = goodMapper.selectBySupplierId(id);
        for (Good good : goods
        ) {
            goodMapper.deleteByPrimaryKey(good.getGoodId());
        }
        List<GoodOrder> goodOrders = goodOrderMapper.selectBySupplierId(id);
        for (GoodOrder goodOrder : goodOrders) {
            goodOrderMapper.deleteByPrimaryKey(goodOrder.getGoodOrderId());
        }
        supplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Supplier findSupplierById(Integer id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateSupplierInfo(Supplier supplier) {
       supplierMapper.updateByPrimaryKeySelective(supplier);
    }

    public void getEventService() {
    }
}

